﻿Imports System.ComponentModel.Composition

'Note. This add-in using the Multipoint Mouse SDK.: http://www.microsoft.com/multipoint/mouse-sdk/
Imports Microsoft.Multipoint.Sdk
Imports System.Drawing

<AddInExport("{ce185f98-5e0e-43b1-9d82-99500979cf45}", "Multipoint viewer",
    Description:="Display and pick messages. ",
    HasSetting:=True, HasWindow:=True)>
<Export(GetType(IDestinationAddIn))>
Public Class MultipointViewer
    Implements IDestinationAddIn

    Private ViewModel As New ViewModel.SettingWindowViewModel

    Public Event Deactivated(ByVal sender As Object, ByVal e As System.EventArgs) Implements IAddIn.Deactivated
    Private WithEvents DisplayWindow As DisplayWindow

    Public Sub Activate() Implements IAddIn.Activate
        If DisplayWindow Is Nothing Then
            DisplayWindow = New DisplayWindow
            DisplayWindow.DataContext = ViewModel
            DisplayWindow.Show()

            Me.PostMessage(Me, New Message() With {.Text = "*Press [ESC] to exit.", .Motion = MessageMotion.StackBottom})

            MultipointSdk.Instance.Register(DisplayWindow)
            AddHandler MultipointSdk.Instance.DeviceArrivalEvent, AddressOf MultipointDeviceArrivalEvent
            AddHandler MultipointSdk.Instance.DeviceRemoveCompleteEvent, AddressOf MultipointDeviceRemoveCompleteEvent
        Else
            DisplayWindow.Activate()
        End If
    End Sub

    Private Sub MultipointDeviceArrivalEvent(ByVal sender As Object, ByVal e As DeviceNotifyEventArgs)

        Me.AddDevice(e.DeviceInfo)

    End Sub

    Private _colorMap() As Color = {
        Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange,
        Color.SkyBlue, Color.GreenYellow, Color.Magenta, Color.DarkCyan, Color.Gold
    }

    Private Sub AddDevice(ByVal device As DeviceInfo)

        Dim col = _colorMap(device.Id Mod 10)

        Dim bmp = New Bitmap(64, 64)
        Dim arrowPoints = New Point() _
                {
                    New Point(5, 36), New Point(5, 5), New Point(36, 5),
                    New Point(31, 10),
                    New Point(51, 30), New Point(30, 51),
                    New Point(10, 31)
                }

        Dim shadowPoints = New Point() _
                {
                    New Point(6, 40), New Point(6, 9), New Point(37, 9),
                    New Point(32, 14),
                    New Point(52, 34), New Point(31, 55),
                    New Point(11, 35)
                }

        Using g = Graphics.FromImage(bmp)

            Dim pen2 = New Pen(Color.FromArgb(100, Color.Black), 5) With {
                .LineJoin = Drawing2D.LineJoin.Round
            }
            g.DrawPolygon(pen2, shadowPoints)

            Dim brush2 = New SolidBrush(Color.FromArgb(100, col))
            g.FillPolygon(brush2, arrowPoints)

            Dim brush1 = New SolidBrush(col)
            Dim pen1 = New Pen(brush1, 5) With {
            .LineJoin = Drawing2D.LineJoin.Round
            }
            g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
            g.DrawPolygon(pen1, arrowPoints)

        End Using

        device.DeviceVisual.CursorBitmap = bmp

    End Sub

    Private Sub MultipointDeviceRemoveCompleteEvent(ByVal sender As Object, ByVal e As DeviceNotifyEventArgs)
        'Throw New NotImplementedException
    End Sub

    Public Sub Deactivate() Implements IAddIn.Deactivate

        If DisplayWindow IsNot Nothing Then

            MultipointSdk.Instance.Dispose()

            DisplayWindow.Close()
            DisplayWindow = Nothing

            MessageBox.Show("Press Alt + F4, please exit the application." & vbCrLf &
                            "(Even if the mouse is disabled when the application exits.)" & vbCrLf & vbCrLf &
                            "and to close this window you must press the Enter key.",
                            "Information", MessageBoxButton.OK, MessageBoxImage.Information)
        End If

    End Sub

    Public Sub ShowDialog(ByVal owner As System.Windows.Window) Implements IAddIn.ShowDialog

        Dim window = New SettingWindow
        window.Owner = owner
        If DisplayWindow IsNot Nothing Then
            window.DisplayWindow = DisplayWindow
        End If

        window.DataContext = ViewModel
        window.Show()

    End Sub

    Public Sub PostMessage(ByVal sourceAddIn As IAddIn, ByVal message As Message) Implements IDestinationAddIn.PostMessage

        If DisplayWindow Is Nothing Then
            Exit Sub
        End If
        DisplayWindow.Add(message)
    End Sub

    Public Sub WriteLog(ByVal sourceAddIn As IAddIn, ByVal log As Log) Implements IDestinationAddIn.WriteLog

    End Sub

    Private Sub DisplayWindow_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles DisplayWindow.Closed
        Deactivate()
    End Sub

    Public Sub Initialize(ByVal settings As ISettings) Implements IAddIn.Initialize

    End Sub

End Class
